/*
 * @Description:
 * @Version: 2.0
 * @Date: 2024-04-09 15:03:16
 * @LastEditTime: 2024-06-06 12:32:57
 */
/*
 * @Date: 2024-04-09 11:44:01
 * @LastEditTime: 2024-04-09 14:06:00
 * @FilePath: \ChatGpt-AI-Vue-Vant\src\router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * 补充：**路由hash模式与history模式的区别
    hash模式
    示例：http://localhost:5173/#/login
    原理：利用window监听的onhashchange事件，不会包含在http请求中，对后端完全没有影响。
    history模式
    示例：http://localhost:5173/login
    原理：地址发生改变后，会按照改变后的地址向服务端发送请求，需要后端配合处理，做地址映射。
 */

import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import { Routes } from "./routes";
import { local } from "@/utils/storage";
import { AccountApi } from "@/api/api";

const routes: Array<RouteRecordRaw> = [
  // 项目打开后进入的默认地址
  {
    path: "/",
    redirect: "/login",
  },
  // 登录
  {
    path: Routes.LOGIN,
    name: "Login",
    component: () => import("@/views/login/Login.vue"),
    meta: {
      title: "登录",
      activeMenuName: "Login",
    },
  },
  // 忘记密码
  {
    path: Routes.FORGET_PWD,
    name: "ForgetPassword",
    component: () => import("@/views/login/forget_password/ForgetPassword.vue"),
  },
  // 注册
  {
    path: Routes.REGISTER,
    name: "Register",
    component: () => import("@/views/register/Register.vue"),
    meta: {
      title: "注册",
      activeMenuName: "Register",
    },
  },
  //   首页
  {
    path: Routes.HOME,
    name: "Home",
    component: () => import("@/views/home/Home.vue"),
    meta: {
      title: "首页",
      activeMenuName: "Home",
    },
  },
  // {
  //   path: Routes.MINE_CENTER,
  //   name: "MineCenter",
  //   component: MineCenter,
  //   redirect: Routes.MINE_CENTER + Routes.ACCOUNT_INFO,
  //   meta: { title: "个人中心", keepAlive: true, activeMenuName: "MineCenter" },
  //   children: [
  //     {
  //       path: Routes.ACCOUNT_INFO,
  //       name: "AccountInfo",
  //       component: AccountInfo,
  //       meta: {
  //         title: "用户信息",
  //         keepAlive: true,
  //         activeMenuName: "MineCenter",
  //       },
  //     },
  //   ],
  // },
  {
    path: Routes.NOT_FOUND,
    name: "404",
    component: () => import("@/views/exception/404/Index.vue"),
  },

  {
    path: Routes.SERVER_ERROR,
    name: "500",
    component: () => import("@/views/exception/500/Index.vue"),
  },
  //   错误路由
  {
    path: Routes.ABSOLUTE_ERROR,
    name: "NotFound",
    redirect: "/404",
  },
];
const router = createRouter({
  history: createWebHashHistory(), // 本项目采用了哈希模式
  routes, 
});

// to要去的路由信息
// from要离开的路由信息
// next是否要放行，默认放行，next(false)不放行
router.beforeEach(async (to, from, next) => {
  if (to.path == "/login" || to.path == "/register" || to.path == "/404" || to.path == "/500") {
    next();
  } else {
    const token = local.get("token");
    const req = {
      token: token
    }
    // token存在则放行
    if (token) {
      try {
        // 向后端发送请求验证 token
        await AccountApi.verifyToken(req);
        next()
      } catch (error) {
        console.error("Token verification failed:", error);
        window.localStorage.removeItem("token");
        next('/login');
      }
    } else {
      next("/login");
    }
  }
});
export default router;
